﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Modeling_Sub_Pages_NewsPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        UniversityDatabaseManagerment udm = new UniversityDatabaseManagerment();

        if (Session["WelcomeUser"] != null)
        {
            if (Session["EmployeeID"] != null && Session["Role"].ToString().Equals("EAS"))
            {
                if (!IsPostBack)
                {
                    GenerateDates gd = new GenerateDates();

                    // Lấy toàn bộ năm đổ vào ddlYear
                    ddlYear.DataSource = gd.generateYear();
                    ddlYear.DataTextField = "Text";
                    ddlYear.DataValueField = "Value";
                    ddlYear.DataBind();
                }
            }
            else
            {
                Response.Redirect("~/LoginMessage.aspx?id=1");
            }
        }
        else
        {
            Response.Redirect("~/Login.aspx?id=2");
        }
    }
    protected void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
    {
            GenerateDates gd = new GenerateDates();

            int year = Convert.ToInt32(ddlYear.SelectedValue.ToString());
            int month = Convert.ToInt32(ddlMonth.SelectedValue.ToString());

            // Lấy toàn bộ năm đổ vào ddlDay
            ddlDay.DataSource = gd.generateDay(year, month);
            ddlDay.DataTextField = "Text";
            ddlDay.DataValueField = "Value";
            ddlDay.DataBind(); 
    }
    protected void ddlYear_SelectedIndexChanged(object sender, EventArgs e)
    {
            GenerateDates gd = new GenerateDates();

            // Lấy 12 tháng đổ vào ddlMonth
            ddlMonth.DataSource = gd.generateMonth();
            ddlMonth.DataTextField = "Text";
            ddlMonth.DataValueField = "Value";
            ddlMonth.DataBind();

            int year = Convert.ToInt32(ddlYear.SelectedValue.ToString());
            int month = Convert.ToInt32(ddlMonth.SelectedValue.ToString());

            // Lấy toàn bộ năm đổ vào ddlDay
            ddlDay.DataSource = gd.generateDay(year, month);
            ddlDay.DataTextField = "Text";
            ddlDay.DataValueField = "Value";
            ddlDay.DataBind(); 
    }
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (args.Value.Length <= 15)
            args.IsValid = true;
        else
            args.IsValid = false;
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        txtFirstName.Text = "";
        txtLastName.Text = "";
        txtStreetAdress.Text = "";
        txtState.Text = "";
        txtCity.Text = "";
        txtPhoneNumber.Text = "";
        txtEmail.Text = "";
    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        UniversityDatabaseManagerment udm = new UniversityDatabaseManagerment();

        string firstName = txtFirstName.Text;
        string lastName = txtLastName.Text;
        string street = txtStreetAdress.Text;
        string state = txtState.Text;
        string city = txtCity.Text;
        string phoneNumber = txtPhoneNumber.Text;
        string email = txtEmail.Text;
        string filePath = "~/Image/noimage.jpg";
        int gender = 0;

        if (rdbFemale.Checked)
        {
            gender = 1;
        }

        GenerateDates gd = new GenerateDates();

        string nextID = gd.generateStuID(udm.showStudentsForMaintainIDDESC(udm.createConnection()).Tables[0].Rows[0][0].ToString());

        string username = firstName + "_" + nextID;

        int year = Convert.ToInt32(ddlYear.SelectedValue.ToString());
        int day = Convert.ToInt32(ddlDay.SelectedValue.ToString());
        int month = Convert.ToInt32(ddlMonth.SelectedValue.ToString());

        string pass = gd.EncodePassword("123456");

        DateTime dateOfBirth = new DateTime(year, month, day);

        if (txtPhoneNumber.Text.Length <= 15 && udm.checkEmail(udm.createConnection(), email))
        {
            if (fuImage.HasFile)
            {
                CheckValid cv = new CheckValid();

                if (cv.CheckFileType(fuImage.FileName))
                {
                    filePath = "~/Image/" + fuImage.FileName;

                    if (System.IO.File.Exists(MapPath(filePath)))
                    {
                        int counter = 2;

                        string tempFileName = "";

                        while (System.IO.File.Exists(MapPath(filePath)))
                        {
                            tempFileName = counter.ToString() + fuImage.FileName;

                            filePath = "~/Image/" + tempFileName;

                            counter++;
                        }
                    }

                    fuImage.SaveAs(MapPath(filePath));
                }
            }

            if (!udm.addStudent(udm.createConnection(),nextID, firstName, lastName,username,pass,gender, dateOfBirth, street, state, city, phoneNumber, email, filePath))
            {
                Response.Redirect("MaintainMessage.aspx?ID=16");
            }
            else
            {
                Response.Redirect("MaintainMessage.aspx?ID=15");
            }
        }
    }
    protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)
    {
        UniversityDatabaseManagerment udm = new UniversityDatabaseManagerment();

        string email = txtEmail.Text;

        if (udm.checkEmail(udm.createConnection(), email))
            args.IsValid = true;
        else
            args.IsValid = false;
    }
}